import { http } from '@/utils/http'
import { defineStore } from 'pinia'
import type ValvePageData from '@/types/valvepage' // 注意类型导入语法
import { reactive } from 'vue'

// 组合式 API
export const useValveTaskFavorStore = defineStore(
  'getValveTaskFavorData',
  () => {
    // State 初始化
    const state = reactive({
      taskData: null as ValvePageData | null, // 假设你需要存储获取到的数据
      loading: false, // 加载状态
      error: null as string | null, // 错误信息
    })

    // Actions
    async function getTaskFavorData(start: string, end: string): Promise<ValvePageData | null> {
      try {
        state.loading = true
        state.error = null

        // 发送请求
        const res = await http<any>({
          method: 'POST',
          url: `/valve/getCollectDataByStartAndEnd?start=${encodeURIComponent(
            start,
          )}&end=${encodeURIComponent(end)}`,
        })

        state.taskData = res.data
        return res.data
      } catch (error) {
        console.error('请求失败:', error)
        state.error = '请求失败，请稍后再试。'
        throw new Error('请求失败') // 抛出错误以便在调用处处理
      } finally {
        state.loading = false
      }
    }

    return { state, getTaskFavorData }
  },
  {
    // 如果需要持久化状态，请取消注释以下行并配置相应的插件
    // persist: true,
  },
)
